/**
 * 
 */
package com.cnhaimei.vod.net.dq;

import android.util.Log;

import com.cnhaimei.com.VODLog;
import com.cnhaimei.vod.bean.Song;
import com.cnhaimei.vod.net.Data;
import com.cnhaimei.vod.net.dq.DQQueryResult.Field;
import com.cnhaimei.vod.net.temp.DQTable;
import com.cnhaimei.vod.util.Util;

/**
 * 歌曲解析
 * 
 * @author jin001.wu@gmail.com <br>
 * @date 2011-7-30
 * 
 */
public class SongParser extends DQParser {
	private static final String TAG = "SongParser";
	DQQueryResult result;

	public SongParser(DQQueryResult result) {
		super(result.getSnap());
		this.result = result;
	}

	@Override
	public Data parse(byte[] in, DQHead head) {
		DQSongs rtn = new DQSongs();
		rtn.snap = head.snap;
		rtn.begin = Util.readInt(in, 0);
		rtn.count = Util.readInt(in, 4);
		
		if (Util.DEBUG) {
			Log.e(TAG, "snap:" + rtn.snap);
			Log.e(TAG, "begin:" + rtn.begin);
			Log.e(TAG, "count:" + rtn.count);
		}
		if (Util.VLOG) {
			VODLog.e(TAG, " snap:" + rtn.snap);
			VODLog.e(TAG, " begin:" + rtn.begin);
			VODLog.e(TAG, " count:" + rtn.count);
		}

		Field nameField = result.getField(DQTable.SongCol.NAME);
		Field authorField = result.getField(DQTable.SongCol.STAR);
		Field vtypeField = result.getField(DQTable.SongCol.VTYPE);
		Field numField = result.getField(DQTable.SongCol.NUMB);
		Field langField = result.getField(DQTable.SongCol.LANG);
		Field aEffF = result.getField(DQTable.SongCol.AEFFECT);
		Field vEffF = result.getField(DQTable.SongCol.VEFFECT);
		Field volmF = result.getField(DQTable.SongCol.VOLUME);
//		Field volvF = result.getField(DQTable.SongCol.UEFFECT);// 
		Field trackF = result.getField(DQTable.SongCol.TRACKS);

		for (int i = 0; i < rtn.count; i++) {
			Song s = new Song();
			String name = "";
			String star = "";
			String vType = "";
			String num = "";
			String lang = "";
			byte aEff = (byte)0;
			byte vEff = (byte) 0;
			int vol = 0;
			byte track = (byte)0;
			
			if (nameField != null) {
				name = Util.readString(in, 8 + i * result.getRecordBytes()
						+ nameField.posi, DQManager.ENCODING);
			}
			if (authorField != null) {
				star = Util.readString(in, 8 + i * result.getRecordBytes()
						+ authorField.posi, DQManager.ENCODING);
			}
			if (vtypeField != null) {
				vType = Util.readString(in, 8 + i * result.getRecordBytes()
						+ vtypeField.posi, DQManager.ENCODING);
			}
			if (numField != null) {
				num = Util.readString(in, 8 + i * result.getRecordBytes()
						+ numField.posi, DQManager.ENCODING);
			}
			if (langField != null) {
				lang = Util.readString(in, 8 + i * result.getRecordBytes()
						+ langField.posi, DQManager.ENCODING);
			}

			if (aEffF != null) {
				aEff = in[8 + i * result.getRecordBytes() + aEffF.posi];
			}
			if (vEffF != null) {
				vEff = in[8 + i * result.getRecordBytes() + vEffF.posi];
			}
			if (volmF != null) {
				vol = Util.readInt(in, 8 + i * result.getRecordBytes() + volmF.posi);
				//in[8 + i * result.getRecordBytes() + volmF.posi];
			}
//			byte volv = in[8 + i * result.getRecordBytes() + volvF.posi];
			if (trackF != null) {
				track = in[8 + i * result.getRecordBytes() + trackF.posi];
			}
			
			s.setName(name);
			s.setSinger(star);
			if (star != null) {
				s.setSingerName(star.replace("|", " "));
			}
			s.setDefinition(vType);
			s.setNumb(num);
			s.setLang(lang);
			s.setaEff(aEff);
			s.setvEff(vEff);
			s.setVolm((byte)((0xffff & vol) % 100));
			s.setVolv_base((byte)((0xffff & vol) / 100));
			s.setTrck(track);
			s.setIndex(rtn.begin + i);
			if (Util.DEBUG) {
				Log.e(TAG, "name:" + name);
				Log.e(TAG, "star:" + star);
				Log.e(TAG, "vType:" + vType);
				Log.e(TAG, "num:" + num);
				Log.e(TAG, "lang:" + lang);
				Log.e(TAG, "aEff:" + aEff);
				Log.e(TAG, "vEff:" + vEff);
				Log.e(TAG, "volm:" + vol);
				// Log.e(TAG, "volv:" + volv);
				Log.e(TAG, "track:" + track);
			}
			rtn.add(s);
		}
		return rtn;
	}

	public DQQueryResult getResult() {
		return result;
	}

	public void setResult(DQQueryResult result) {
		this.result = result;
//		in = new byte[result.getRecordBytes()];
	}

}
